/**
 * 
 */
package com.java.test.system.mapper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.util.PageInfo;

/**
 * @author 登陆账号
 *
 */
@Component
public class LoginAccountMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectloginaccountCount(LoginAccountInfo info) {

		String sql = "select count(*) from system_account where 1=1";

		if (info.getStatus() != 0) {
			sql += " and status='" + info.getStatus() + "'";
		}

		if (info.getRoleId() != 0) {
			sql += " and role_id='" + info.getRoleId() + "'";
		}

		if (info.getCreateTime() != null) {
			sql += " and TO_DAYS(create_time)= TO_DAYS('" + info.getCreateTime() + "')";
		}

		if (info.getRealName() != null && !"".equals(info.getRealName())) {
			sql += " and real_name like '%" + info.getRealName() + "%'";
		}

		int i = jdbcTemplate.queryForObject(sql, Integer.class);

		return i;
	}

	public List<LoginAccountInfo> selectloginaccountList(LoginAccountInfo info, PageInfo<LoginAccountInfo> page) {

		String sql = "select * from system_account where 1=1 ";

		if (info.getStatus() != 0) {
			sql += " and status='" + info.getStatus() + "'";
		}

		if (info.getRoleId() != 0) {
			sql += " and role_id='" + info.getRoleId() + "'";
		}

		if (info.getCreateTime() != null) {
			sql += " and TO_DAYS(create_time)= TO_DAYS('" + info.getCreateTime() + "')";
		}

		if (info.getRealName() != null && !"".equals(info.getRealName())) {
			sql += " and real_name like '%" + info.getRealName() + "%'";
		}
		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " limit ?,?";

		List<LoginAccountInfo> list = jdbcTemplate.query(sql, new LoginAccountInfo(), start, limit);

		return list;
	}

	public int insertloginaccountInfo(LoginAccountInfo info) {
		String sql = "insert into system_account(real_name,login_account,password,role_id,role_name,create_time,status)"
				+ "values(?,?,?,?,?,?,1)";
		int i = jdbcTemplate.update(sql, info.getRealName(), info.getLoginAccout(), info.getPassWord(),
				info.getRoleId(), info.getRoleName(), info.getCreateTime());
		return i;

	}

	public int deleteloginaccountInfo(int[] ids) {
		String sql = "delete from  system_account where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	/**
	 * 解封
	 * 
	 * @param ids
	 * @return
	 */
	public int updateloginaccountInfouse(int[] ids) {
		String sql = "update system_account set  status=1 where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	/**
	 * 封禁
	 * 
	 * @param ids
	 * @return
	 */
	public int updateloginaccountInfonotuser(int[] ids) {
		String sql = "update system_account set  status=2 where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	public int updateloginaccountPassword(LoginAccountInfo info) {

		String sql = "update system_account set password=? where id=?";

		int i = jdbcTemplate.update(sql, info.getPassWord(), info.getId());
		return i;
	}

	public LoginAccountInfo loginuser(String userName, String password) {

		String sql = "select * from system_account where login_account=? and password=?";

		LoginAccountInfo info = null;
		try {
			info = jdbcTemplate.queryForObject(sql, new LoginAccountInfo(), userName, password);
		} catch (Exception e) {
			if (e instanceof EmptyResultDataAccessException)
				return null;
		}
		
		return info;
	}

	
	public LoginAccountInfo loginuserisexits(String userName) {

		String sql = "select * from system_account where login_account=?";

		LoginAccountInfo info = null;
		try {
			info = jdbcTemplate.queryForObject(sql, new LoginAccountInfo(), userName);
		} catch (Exception e) {
			if (e instanceof EmptyResultDataAccessException)
				return null;
		}	
		return info;
	}
	
}
